package com.yellowpage.ca.action;
  
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.ca.data.UserFunction;
import com.yellowpage.ca.service.UserFunctionService;

public class UserFunctionAction extends BaseAction {

    public String RESULT_QUERY = "query";
    public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	
	private Configuration configuration;
	
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}
	
	private List<UserFunction> userFunctionList = new ArrayList<UserFunction>();
	private List<Map> resultList = new ArrayList<Map>();
	
	private UserFunction userFunction;
	private UserFunctionService userFunctionService;
	
	private String selectedId;	
	private String[] selectedIds;	
	private String message;
	
		private String query_userFunctionId;
		private String query_userId;
		private String query_functionId;

	public Map getFunctionMap() {
		return functionMap;
	}

	public void setFunctionMap(Map functionMap) {
		this.functionMap = functionMap;
	}

	public Map getUserMap() {
		return userMap;
	}

	public void setUserMap(Map userMap) {
		this.userMap = userMap;
	}

	private Map functionMap;
		private Map userMap;

	public void prepare() {
		super.prepare();
	
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.userFunction = userFunctionService.getUserFunction(Long.parseLong(selectedId));
		}


		//给出了useradd界面的两个下拉框的值
		functionMap = userFunctionService.getFunctionMap();
		userMap	= userFunctionService.getUserMap();
	}
	
	public String queryByPage() {		
		initPageRender();
		
		Map queryMap = new HashMap();	
			    queryMap.put("userFunctionId", query_userFunctionId);
	    	    queryMap.put("userId", query_userId);
	    	    queryMap.put("functionId", query_functionId);
	    	    	
		resultList = userFunctionService.queryUserFunctionByPage(queryMap, pageRender);
		
		setPageRenderToRequest();

		return RESULT_QUERY;
	}
	
	public String toAdd() {
		return RESULT_ADD;
	}
	
	public String save() {
	    
		userFunctionService.saveUserFunction(userFunction);		
		
		message = "新增成功!";
		return RESULT_ADD;
	}
	
	public void validateSave() {
	    	    	    	    	    	    	    	    
		this.setValidatorResult("/WebMoudule/ca/UserFunctionAdd.jsp");		
	}
	
	public String toEdit() {
	    Long id = new Long(selectedId);
	    
	    userFunction = userFunctionService.getUserFunction(id);
	
		return RESULT_EDIT;
	}
	
	public String update() {
	
	    userFunctionService.updateUserFunction(userFunction);	

		message = "修改成功!";
	    
		return RESULT_EDIT;
	}
	
	public void validateUpdate() {
					    			    			    		this.setValidatorResult("/WebMoudule/ca/UserFunctionUpdate.jsp");	
	}
	
	public String delete() {	    
	    List ids = new ArrayList();
	    
		for(String id : selectedIds){
			ids.add(Long.valueOf(id));
		}
		
		try {
			this.userFunctionService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("");
		}
		
		
		return queryByPage();
	}	
	
	

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	 public void setUserFunction(UserFunction userFunction) {
		this.userFunction = userFunction;
	}
	
	public UserFunction getUserFunction() {
		return userFunction;
	}
	 
	 
	 public void setUserFunctionService(UserFunctionService userFunctionService) {
		this.userFunctionService = userFunctionService;
	}
	
	public List<UserFunction> getUserFunctionList() {
		return userFunctionList;
	}

	public void setUserFunctionList(List<UserFunction> userFunctionList) {
		this.userFunctionList = userFunctionList;
	}
	
	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}
	
	    public String getQuery_userFunctionId() {
        return this.query_userFunctionId;
    }
    
    public void setQuery_userFunctionId(String userFunctionId){
        this.query_userFunctionId = userFunctionId;
    }    

        public String getQuery_userId() {
        return this.query_userId;
    }
    
    public void setQuery_userId(String userId){
        this.query_userId = userId;
    }    

	public String getQuery_functionId() {
        return this.query_functionId;
    }
    
    public void setQuery_functionId(String functionId){
        this.query_functionId = functionId;
    }    

    
	
}
